package com.pocketsweet.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.C;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLConsumeRecord;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.AutoListView;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.AutoListView.OnLoadListener;
import com.pocketsweet.ui.uilib.AutoListView.OnRefreshListener;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.adapter.RechargeListAdapter;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_recharge_record)
public class RechargeRecord extends BaseActivity implements OnRefreshListener,
		OnLoadListener {

	@ViewInject(R.id.RechargeRecordListview)
	private AutoListView RechargeRecordListview;

	@ViewInject(R.id.mainHeader)
	private HeaderLayout header;

	public static LoadingDailog loading;
	private RechargeListAdapter rechargeListAdapter;
	private List<MLConsumeRecord> rechargeRecord = new ArrayList<MLConsumeRecord>();
	private Context ctx;
	public static final int BEGIN_TO_LOADING = -1;
	private int skipDataCount = 0; // 忽略前N个对象
	public static final int MORE_DATA_MAX_COUNT = 10; // 每页10个对象
	private boolean isUpdateList = false; // 是否刷新列表

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		ctx = this;
		rechargeListAdapter = new RechargeListAdapter(RechargeRecord.this,
				rechargeRecord);
		RechargeRecordListview.setAdapter(rechargeListAdapter);
		RechargeRecordListview.setPageSize(MORE_DATA_MAX_COUNT);
		RechargeRecordListview.setOnRefreshListener(this);
		RechargeRecordListview.setOnLoadListener(this);
		setHeaderFunction();
		getRecordList(AutoListView.REFRESH);
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("");
		header.setMiddleText("充值记录");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				RechargeRecord.this.finish();
			}
		});
	}

	private void getRecordList(final int what) {
		loading = ToolKits.createLoadingDialog(RechargeRecord.this, "正在获取中");
		if (what != BEGIN_TO_LOADING) {
			isUpdateList = true; // 如果不是首次加载数据，则该动作为刷新
		} else {
			RechargeRecordListview.setRefreshData(); // 初始化显示loading动态图片
		}
		if (rechargeRecord == null || rechargeRecord.size() <= 0
				|| isUpdateList) { // userList为空或者动作为下拉刷新、往下加载时请求leanCloud

			AVQuery<MLConsumeRecord> querys = new AVQuery<MLConsumeRecord>(
					"MLConsumeRecord");
			skipDataCount = skipDataCount + MORE_DATA_MAX_COUNT; // 往下加载时忽略前一页
			if (what != AutoListView.LOAD) {
				skipDataCount = 0; // 刷新时不用忽略前面对象
			}
			querys.whereEqualTo("user", UserService.getCurrentUser());
			querys.whereEqualTo("type", C.CONSUME_TYPE_RECHARGE);
			querys.orderByDescending("createdAt");
			querys.setSkip(skipDataCount); // 忽略前N个
			querys.limit(MORE_DATA_MAX_COUNT); // 设置每页对象个数
			querys.findInBackground(new FindCallback<MLConsumeRecord>() {

				@Override
				public void done(List<MLConsumeRecord> records, AVException e) {
					if (e == null) {
						if (records.size() > 0) {
							switch (what) {
							case AutoListView.REFRESH: // 下拉刷新时直接重新获取数据
								rechargeRecord = records;
								RechargeRecordListview.onRefreshComplete();
								// onLineListAdapter.setDatas(userList);
								break;
							case AutoListView.LOAD: // 加载更多时把新获取的数据添加到list列表
								RechargeRecordListview.onLoadComplete();
								rechargeRecord.addAll(records);
								break;
							case BEGIN_TO_LOADING: // Application无数据时首次加载，即重新获取数据
								rechargeRecord = records;
								RechargeRecordListview.onRefreshComplete();
								// onLineListAdapter.setDatas(userList);
								break;
							}
							RechargeRecordListview.setResultSize(rechargeRecord
									.size());
							rechargeListAdapter.setDatas(rechargeRecord);
							rechargeListAdapter.notifyDataSetChanged();
							isUpdateList = false; // 刷新完毕，设为false
						} else if (records.size() == 0) {
							if (what != AutoListView.LOAD) {
								ToolKits.toast(RechargeRecord.this, "没有充值记录");
							}
							RechargeRecordListview.setNoData();
						}
					} else {
						// 已加载完全部，取消loading
//						Log.e("====mlFriendApplies", "nothing" + e.getMessage());
						getRecordList(BEGIN_TO_LOADING);

					}
				}

			});
		}
	}

	@Override
	public void onLoad() {
		getRecordList(AutoListView.LOAD);

	}

	@Override
	public void onRefresh() {
		getRecordList(AutoListView.REFRESH);
		RechargeRecordListview.setOnRefreshListener(this);

	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
